/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-08-22 15:40:56
 * @filename : static/mnm/src/views/changePassword.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module){

        require("{resourceUrl}css/mnm/public.css");

        var CgPWView,
            $ = require('$'),
	    Backbone = require('backbone'),
            _ = require('underscore'),
            util = require('{baseSea}util/util'),
            share = require('../share'),
            lang = require('{baseSea}I18N/mnm/{locale}'),
            tpl = require('{resourceUrl}tpl/mnm/changePassword.tpl');
	
        CgPWView = Backbone.View.extend({

		el: 'body',

		template: _.template(tpl),

		events: {
                    "tap .back"         : "goBack",  
                    "tap .btn-changepw" : "changePw" 	        
                },

		initialize: function () {
                
                },

		render: function (obj) {
                    if ($('#changePassword').length < 1) { 
                        $('body').append(this.template(lang));  
                    } else {
		        $('#changePassword').show();
                    }
                    $('#body_loading').hide();
                },

                changePw : function() {
                    var obj = {};

                        obj.passwordO = $('input[name="passwordO"]').val();
                        obj.passwordN = $('input[name="passwordN"]').val();
                        obj.passwordC = $('input[name="passwordC"]').val();

			if (this.validata(obj)) {
			    this.ajaxChangePw(obj);
			}
                },

                goBack : function() {
                     //window.location.href = '#signin/whole'; 
                     window.history.go(-1); 
                },
 
                validata : function(data) {
                        var bool = true,obj = {bool:true,type:1};
         
			    for (var i in data) {
			        if ($.trim(data[i]) == '') {
				    obj.bool = false;
			            obj.msg = lang.required; 
				 } else if (!util.isLength(data[i],6,18)) {
				    obj.bool = false;
				    obj.msg = lang.password_format_error+'[6-18].';
				 } else if (i == 'passwordC'){
				     if(data['passwordC'] !== data['passwordN']) {
					obj.bool = false;
					obj.msg = lang.passwordConfirm;
				     } 
			        };
			        obj.dom = $('.'+i);
			        share.inputError(obj);
                                if (!obj.bool) {
                                   bool = false;
                                }
                           }		
                        return bool;
                },
 
                ajaxChangePw : function(data) {
                     var dom = $('.btn-changepw');
                         dom.attr('ajax',1);
		         share.ajaxStartA(dom);
		     var obj = {
			 url : seajs.data.vars.apiUrl+'user_register',
			 type : 'POST',
			 data : data,
			 success : function(data) {
				 if (data.error == 0 ) {
				 } 
			 },
			 error : function(xhr, errorType, error) {
				 share.error('alert','http request failed!'); 
			 },
			 complete : function(xhr,status){
			     dom.attr('ajax',0);
			     share.ajaxEndA(dom);
			 } 
		    };
                    //$.ajax(util.ajaxControl(obj)); 
                }
 
	});

	module.exports = CgPWView;
});

